package com.flute.haflute.agent.job;

import java.util.Map;
import java.util.WeakHashMap;

import com.flute.haflute.agent.topo.Node;
import com.flute.haflute.jobbox.base.CloudJobConf;

/**
 * 管理JOB的集群环境
 * 
 * 1. 运行上下文环境建立
 * 
 * 2. 准备其他资源、如文件传送、配置同步等
 * 
 * 3. 保存待执行JOB（等待列表）
 * 
 * 4. 监听集群任务资源的可用性。
 */
public class RootJobScheduler {

	private Map<String, Node> jobRequest2MasterCache = new WeakHashMap<String, Node>();

	public synchronized Node initialContextAndNotifyNodes(CloudJobConf request)
			throws Exception {
		Node master = JobContextInstaller.prepare4NewJob(request);
		jobRequest2MasterCache.put(request.getJobDescription(), master);
		return master;
	}

	public synchronized void clearCache(CloudJobConf request) {
		jobRequest2MasterCache.remove(request.getJobDescription());
	}

	public synchronized void recoverNodesInCache(CloudJobConf request) {
		Node master = jobRequest2MasterCache
				.remove(request.getJobDescription());
		if (master == null) {
			return;
		}
		JobResourceManager.getInstance().releaseNodes4Job(master);
	}
}
